#include "clientHead/order_analyse.h"

int order_judg(train_chdata* ch, order_train_t* od){
    char buf[100] = {0};
    char command[20] ={0};
    char* arg;
    
    strcpy(buf, ch);
    buf[strlen(buf) - 1] = '\0';
    // 切出指令
    strcpy(command, strtok(buf, " "));
    arg = strtok(NULL, " ");
    od->order_len = orderAnalHash(command);
    switch(od->order_len){
        case ORDER_ERROR:
            showInvaild();
            return -1;
        case ORDER_LS: case ORDER_PWD:
            if(arg != NULL){
                showArgDontNeed();
                return -1;  
            }
            od->data_len = 6;
            strcpy(od->train_data, "No arg");
            break;
        default:
            if(arg == NULL){
                showArgNeed();
                return -1;
            }
            od->data_len = strlen(arg);
            strcpy(od->train_data, arg);
            printf("od->train:%s\n", od->train_data);
            break;
    }
    return 0;
}


int hashOpcode(train_chdata* ch){
    int hashValue = 0;
    char *p =ch;
    while (*p != '\0') {
        hashValue += (int) *p++;
    }
    return hashValue % HASH_COUNT;
}
 
TokenType_ orderAnalHash(train_chdata* ch){
    int hash=hashOpcode(ch);
    if(strcmp(order_list[hash].orderCh, ch) == 0) {
        // 哈希判断指令是否存在
        return order_list[hash].orderType;
    }
    return ORDER_ERROR;
}

